﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using SuperMap.Data;
using SuperMap.UI;
namespace MDF
{
    /// <summary>
    /// 属性修改
    /// </summary>
    public partial class SJJZSX : CCSkinMain
    {
        private object cellValue;
        private DatasetVector m_datasetVector;
        private DataManage m_dataManage;
        /// <summary>
        /// 属性修改窗体初始化
        /// </summary>
        /// <param name="workspace">工作空间树</param>>
        public SJJZSX(Workspace workspace)
        {
            InitializeComponent();
            this.workspaceTree.Workspace = workspace;
            
            Hide_UI();
        }
        /// <summary>
        /// 隐藏树视图的显示
        /// </summary>
        private void Hide_UI()
        {
            try
            {
                //移除不必要节点
                for (int i = 1; i < 5; i++)
                {
                    this.workspaceTree.Nodes[0].Nodes[1].Remove();
                }

                this.workspaceTree.WorkspaceNode.Text = "工作空间";
                this.workspaceTree.DatasourcesNode.Text = "数据源";
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 单击节点时显示信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void workspaceTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                if (this.workspaceTree.SelectedNode.Level > 2)
                {
                    //获取节点对应数据集
                    WorkspaceTreeNodeBase workspaceTreeNodeBase = (WorkspaceTreeNodeBase)this.workspaceTree.SelectedNode;
                    //获取数据集字段信息及其信息
                    m_datasetVector = workspaceTreeNodeBase.GetData() as DatasetVector;
                    foreach (FieldInfo info in m_datasetVector.FieldInfos)
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        row.CreateCells(this.FieldGridView);
                        //填充值
                        row.Cells[0].Value = info.Caption;
                        row.Cells[1].Value = info.Type;
                        row.Cells[2].Value = info.MaxLength;
                        row.Cells[3].Value = info.IsRequired;
                        row.Cells[4].Value = info.DefaultValue;
                        this.FieldGridView.Rows.Add(row);
                    }
                    //获取属性信息
                    m_dataManage = new DataManage();
                    Task task = new Task(() => m_dataManage.OpenDataTable(this.AttributeGridView, workspaceTreeNodeBase));
                    task.Start();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 切换选项卡时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.Save_Button.Visible)
                {
                    //获取到切换后的index值，并取消单元格可编辑
                    if (tabControl.SelectedIndex == 1)
                    {
                        this.FieldGridView.ReadOnly = true;
                    }
                    else
                    {
                        this.AttributeGridView.ReadOnly = true;
                    }
                    //不可用保存
                    this.Save_Button.Enabled = false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Revise_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //使单元格可编辑
                if(tabControl.SelectedIndex == 0)
                {
                    
                    //判断是否选择单元格
                    if (this.FieldGridView.SelectedCells.Count > 0)
                    {
                        //获取字段序号
                        int index = this.FieldGridView.SelectedCells[0].RowIndex;
                        //判断是否是系统字段
                        if (m_datasetVector.FieldInfos[index].IsSystemField)
                        {
                            MessageBox.Show("系统字段不能修改！", "警告");
                            return;
                        }
                        else
                        {
                            //获取单元格的旧值
                            cellValue = this.FieldGridView.SelectedCells[0].Value;
                            //修改控件状态
                            this.FieldGridView.ReadOnly = false;
                            this.Save_Button.Enabled = true;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (this.AttributeGridView.SelectedCells.Count > 0)
                    {
                        //获取字段序号
                        int index = this.AttributeGridView.SelectedCells[0].ColumnIndex;
                        //判断是否是系统字段
                        if (m_datasetVector.FieldInfos[index].IsSystemField)
                        {
                            MessageBox.Show("系统字段不能修改！", "警告");
                            return;
                        }
                        else
                        {
                            //获取单元格的旧值
                            cellValue = this.FieldGridView.SelectedCells[0].Value;
                            //修改控件状态
                            this.AttributeGridView.ReadOnly = false;
                            this.Save_Button.Enabled = true;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (tabControl.SelectedIndex == 0)
                {
                    //获取字段序号，属性名称和修改值
                    int index = this.FieldGridView.SelectedCells[0].RowIndex;
                    string attributeName = this.FieldGridView.SelectedCells[0].OwningColumn.Name;
                    object value = this.FieldGridView.SelectedCells[0].Value;
                    //不允许为空值
                    if(value == null) 
                    {
                        this.FieldGridView.SelectedCells[0].Value = cellValue;
                        MessageBox.Show("值不允许为空！", "警告：");
                    }
                    else
                    {
                        //判断是否合法和是否为系统字段
                        if (!m_datasetVector.FieldInfos[index].IsSystemField)
                        {
                            //只允许修改别名
                            m_dataManage.ReviseDatasetInfos(index, attributeName, value, m_datasetVector);

                            this.FieldGridView.ReadOnly = true;
                            this.Save_Button.Enabled = false;
                        }
                        else
                        {
                            return;
                        }
                    }
                    
                }
                else
                {
                    //获取记录序号和修改字段的名称，修改值
                    int recordIndex = this.AttributeGridView.SelectedCells[0].RowIndex;
                    int fieldIndex = this.AttributeGridView.SelectedCells[0].ColumnIndex;
                    object value = this.AttributeGridView.SelectedCells[0].Value;
                    //判断是否合法和是否为系统字段
                    if (!m_datasetVector.FieldInfos[fieldIndex].IsSystemField)
                    {
                        //修改属性值
                        m_dataManage.ReviseDatasetRecord(recordIndex, fieldIndex, value, m_datasetVector);
                        this.AttributeGridView.ReadOnly = true;
                        this.Save_Button.Enabled = false;
                    }
                    else
                    {
                        return;
                    }
                }
                MessageBox.Show("保存完成！", "提示：");
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Exit_Button_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception)
            {

                throw;
            }
        }
        
    }
}
